package com.inet.test07;


import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * 登录页面
 *
 * @Author weijingchen
 * @Date 2025/6/26 15:15
 * @Version 1.0.0
 */
public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 10301);
        System.out.println("==========服务器连接成功==========");

        while (true) {
            System.out.println("==========欢迎来到黑马聊天室==========");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("请输入您的选择：");
            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();
            switch (choice) {
                case 1 -> login(socket);
                case 2 -> register(socket);
                default -> System.out.println("输入错误！请重新输入！");
            }
        }


    }

    private static void register(Socket socket) throws IOException {
        System.out.println("==========注册==========");
        OutputStream os = socket.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();
        System.out.println("请输入密码：");
        String password2 = sc.nextLine();
        if (!password.equals(password2)) {
            System.out.println("密码不一致！请重新输入！");
            register(socket);
        }
        String result = "username=" + username + "&password=" + password;
        // 第一个写执行什么操作
        write(bw, "register");
        // 往服务器发送数据
        write(bw, result);
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String msg = br.readLine();
        if ("1".equals(msg)) {
            System.out.println("注册成功");
            login(socket);
        } else if ("2".equals(msg)) {
            System.out.println("用户名已存在，请重新输入");
            register(socket);
        }

    }


    public static void login(Socket socket) throws IOException {
        System.out.println("==========登录==========");
        OutputStream os = socket.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();
        String result = "username=" + username + "&password=" + password;
        // 第一个写执行什么操作
        write(bw, "login");
        // 往服务器发送数据
        write(bw, result);

        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String msg = br.readLine();
        if ("1".equals(msg)) {
            System.out.println("登录成功,可以开始聊天");
            new Thread(new ClientMyRunnable(socket)).start();
            talk2All(socket);
        } else if ("2".equals(msg)) {
            System.out.println("用户名不存在");
        } else if ("3".equals(msg)) {
            System.out.println("密码错误！");
            login(socket);
        }


    }

    // 聊天
    private static void talk2All(Socket socket) throws IOException {
        Scanner sc = new Scanner(System.in);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        while (true) {
            System.out.println("请输入要发送的内容：");
            String str = sc.nextLine();
            write(bw, str);


        }
    }

    private static void write(BufferedWriter bw, String str) throws IOException {
        bw.write(str);
        bw.newLine();
        bw.flush();
    }
}

class ClientMyRunnable implements Runnable {

    private Socket socket;

    public ClientMyRunnable(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        while (true) {
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String msg = br.readLine();
                System.out.println(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}